<template>
	<view>
		<!-- <u-navbar :title="firstForm.docId?'编辑家庭':'创建家庭'" :leftIcon="type?'arrow-left':''" :placeholder="true"
			leftIconColor="#fff" :autoBack="true" :bgColor="bgColor" :titleStyle="titleStyle"></u-navbar> -->
			<headerinfo></headerinfo>
		<!-- <headertop :inv="4"></headertop> -->
		<backos></backos>
		<view class="form">
			<view class="list">
				<div class="form_label">
					<span class="red">*</span>
					名称
				</div>
				<input type="text" v-model="firstForm.familyName" placeholder="请输入家庭名称">
			</view>
			<view class="list">
				<div class="form_label">家庭简介</div>
				<textarea v-model="firstForm.familyRemark" placeholder="选填"></textarea>
			</view>
			<view class="list">
				<div class="form_label">是否允许家庭成员添加其他家庭成员</div>
				<u-switch v-model="firstForm.allowMemberAddMember" activeColor="#5ac725"></u-switch>
			</view>
			<view class="list">
				<div class="form_label">是否允许家庭成员更新其他家庭成员</div>
				<u-switch v-model="firstForm.allowMemberUpdateMember" activeColor="#5ac725"></u-switch>
			</view>
			<view class="list">
				<div class="form_label">背景图（默认优先背景图）</div>
				<view class="imgList">
					<view class="list nobg" v-if="firstForm.bgimg" @click="viewImg()">
						<i class="iconfont delImg" @click.stop="delImg()">&#xe785;</i>
						<image :src="firstForm.bgimg" class="fileImg" mode="aspectFill"></image>
					</view>
					<view class="list" @click="chooseImage" v-if="!firstForm.bgimg">
						<i class="iconfont">&#xe68e;</i>
						<view class="">添加背景图</view>
					</view>
					<!-- 占位 -->
					<div class="list zhanwei" v-for="index in 4" :key="index"></div>
				</view>
			</view>
			<view class="list">
				<div class="form_label">背景颜色</div>
				<!-- <v587-color-picker v-model="color" /> -->
				<zebra-color-picker v-model="colors"></zebra-color-picker>
			</view>
			<view class="createBtn" @click="createFirstSubmit()">{{firstForm.docId?'保存':'创建'}}</view>
		</view>
		
		<view class="" style="height: 30px;">
			
		</view>
		<w-compress ref='wCompress' />
	</view>
</template>

<script>
	import minxins from '@/common/upload.js';
	import WCompress from '@/components/w-compress/w-compress.vue'
	export default {
		components: {
			WCompress
		},
		mixins: [minxins],
		data() {
			return {
				colors: {
					hex: '#de4849'
				},
				type: '',
				bgColor: '#de4849',
				titleStyle: {
					color: '#FF9D6C '
				},

				firstForm: {
					allowMemberAddMember: true,
					allowMemberUpdateMember: true,
					familyName: '',
					familyRemark: '',
					bgimg: '',
					familyBackground: '',
					familyIcon: '',
					useColorAsBg: false
				},

			};
		},

		onLoad(e) {
			if (e.type) {
				this.type = 1
			}
			if (e.data) {
				this.firstForm = JSON.parse(e.data)
				console.log(this.firstForm)
				if (this.firstForm.useColorAsBg) {
					this.firstForm.bgimg = this.firstForm.familyBackground
				} else {
					this.colors.hex = this.firstForm.familyBackground
				}
			}
		},
		methods: {
			viewImg() {
				uni.previewImage({
					urls: [this.firstForm.bgimg],
					current: 0
				});
			},
			chooseImage() {

				uni.chooseImage({
					count: 1,
					success: res => {
						// #ifdef MP-WEIXIN

						this.$refs.wCompress.start(res.tempFilePaths).then(res2 => {
							res2.forEach((item, index) => {
								var fileName = Date.now() + '_' + Math.round(Math.random() *
									100000000) + '.' + item.substring(item.lastIndexOf(
									'.') + 1)

								var maxImageMegBytes = uni.getStorageSync('maxImageMegBytes') ?
									uni.getStorageSync('maxImageMegBytes') : 20

								var resSize = res.tempFiles[index].size;
								if (resSize > maxImageMegBytes * 1024 * 1024) {
									uni.showToast({
										title: `大小不能超过${maxImageMegBytes}MB`,
										icon: 'none'
									});
									return
								}

								var familyId = uni.getStorageSync('familyId') ? uni
									.getStorageSync('familyId')[0] : ''


								this.uploadUserImage(item, 'image', fileName, familyId, (
									imgUrl) => {
									console.log(imgUrl)
									this.firstForm.bgimg = imgUrl;
									this.firstForm.useColorAsBg = true
								})
							})
						})
						// #endif
						// #ifndef MP-WEIXIN
						for (let i = 0; i < res.tempFiles.length; i++) {
							console.log('res',res)
							let item = res.tempFiles[i]

							console.log(item)

							// #ifdef APP-PLUS
							let houzui = item.path.substring(item.path.lastIndexOf('.') + 1)
							// #endif

							// #ifdef H5
							let houzui = item.name.substring(item.name.lastIndexOf('.') + 1)
							// #endif

							let name = Date.now() + '_' + Math.round(Math.random() * 100000000)
							let path = item.path
							let fileName = name + '.' + houzui

							let maxImageMegBytes = uni.getStorageSync('maxImageMegBytes') ? uni.getStorageSync(
								'maxImageMegBytes') : 20

							let resSize = item.size;
							if (resSize > maxImageMegBytes * 1024 * 1024) {
								uni.showToast({
									title: `大小不能超过${maxImageMegBytes}MB`,
									icon: 'none'
								});
								return
							}



							let familyId = uni.getStorageSync('familyId') ? uni.getStorageSync('familyId')[0] :
								''


							// #ifdef APP-PLUS
							uni.compressImage({
								src: path,
								quality: 30,
								compressedWidth: 300,
								success: res => {
									this.uploadUserImage(res.tempFilePath, 'image', fileName,
										familyId, (
											imgUrl) => {
											this.firstForm.bgimg = imgUrl;
											this.firstForm.useColorAsBg = true
										})
								}
							})
							// #endif
							// #ifdef H5
							//压缩图片
							const tempFilePaths = res.tempFilePaths;
							this.uploadUserImage(tempFilePaths[0], 'image', fileName, familyId, (
								imgUrl) => {
								console.log('this.uploadUserImage path',imgUrl);
								this.firstForm.bgimg = imgUrl;
								this.firstForm.useColorAsBg = true;
								this.$forceUpdate()
							})
							// this.getImageInfo(path).then(fileUrl => {
							// 	this.uploadUserImage(fileUrl, 'image', fileName, familyId, (
							// 		imgUrl) => {
							// 		this.firstForm.bgimg = imgUrl;
							// 		this.firstForm.useColorAsBg = true
							// 	})
							// })
							// #endif




						}
						// #endif

					}
				});


			},
			delImg() {
				this.firstForm.bgimg = '';
				this.firstForm.useColorAsBg = false;
				this.$forceUpdate()
				console.log(this.firstForm)
			},
			createFirstSubmit() {

				console.log(this.colors)

				if (this.firstForm.familyName == '') {
					return this.$Show.show('家庭名称不能为空');
				}

				if (this.firstForm.useColorAsBg) {
					//有背景图
					this.firstForm.familyBackground = this.firstForm.bgimg
				} else {
					// 用颜色
					this.firstForm.familyBackground = this.colors.hex
				}

				if (this.firstForm.docId) {
					this.firstForm.familyId = this.firstForm.docId

					this.$api.updateFamilyName(this.firstForm).then(res => {
						this.$Show.show('修改成功');
						setTimeout(() => {
							uni.navigateBack()
						}, 1500)
					});
				} else {
					this.$api.addFamily(this.firstForm).then(res => {
						this.$Show.show('创建成功');
						uni.setStorageSync('familyId', [res]);
						setTimeout(() => {
							if (this.type) {
								uni.navigateBack()
							} else {
								uni.reLaunch({
									url: '/pages/tabbar/index/index'
								})
							}
						}, 1500)
					});
				}

			},
		}
	};
</script>


<style lang="scss">
	.imgList {
		display: flex;
		align-items: center;
		flex-wrap: wrap;
		justify-content: space-between;

		.list {
			width: 220rpx;
			height: 220rpx;
			display: flex;
			align-items: center;
			background-color: #f8f8f8;
			justify-content: center;
			flex-direction: column;
			border-radius: 5rpx;
			margin-bottom: 20rpx;
			position: relative;

			.delImg {
				font-size: 42rpx;
				position: absolute;
				right: 3px;
				top: 3px;
				color: #fff !important;
				z-index: 10;
			}

			view {
				font-size: 24rpx;
				color: #b7b7b7;
				margin-top: 15rpx;
			}

			.iconfont {
				color: #b7b7b7;
				font-size: 40rpx;
			}

			.fileImg {
				width: 100%;
				height: 100%;
			}
		}

		.nobg {
			background-color: #fff;
		}

		.zhanwei {
			visibility: hidden;
			height: 0;
			margin: 0;
		}
	}

	.form {
		padding: 20rpx;
		box-sizing: border-box;
		padding: 0 30rpx;
		box-sizing: border-box;
		width: 600px;
		padding-top: 20px;
		padding-bottom: 30px;
		margin: 0 auto;
		box-shadow: 0 0 3px #ccc;
		border-radius: 20rpx;
		position: relative;
		

		.list {
			margin-bottom: 40rpx;
		}

		.form_label {
			margin-bottom: 10rpx;
			font-size: 28rpx;
		}

		.red {
			color: red;
		}

		input {
			border: 1px solid #eee;
			border-radius: 10rpx;
			padding: 0 20rpx;
			height: 80rpx;
			font-size: 30rpx;
			width: 100%;
		}

		textarea {
			border: 1px solid #eee;
			border-radius: 10rpx;
			padding: 20rpx;
			height: 300rpx;
			font-size: 30rpx;
			width: 100%;
		}

		.createBtn {
			width: 100%;
			height: 90rpx;
			border-radius: 10rpx;
			display: flex;
			align-items: center;
			margin-top: 30rpx;
			justify-content: center;
			box-sizing: border-box;
			background-color: #de4849;
			color: #fff;
		}

	}
</style>